package com.cs4590.finalProject.userInterface;

import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.cs4590.finalProject.R;

public class Chords extends Activity {

	SoundPool sp;
	int explosion = 0;
	String chord, scale;
	ImageView image, image2;
	
	String name;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.page_chords);

		sp = new SoundPool(3,AudioManager.STREAM_MUSIC,0);
		explosion = sp.load(Environment.getExternalStorageDirectory().getAbsolutePath() + "/default.mp3", 0);//explosion = sp.load(Chords.this, R.raw.fish, 0);

		Spinner chordSpinner = (Spinner) findViewById(R.id.chords_spinner);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
				R.array.chords_array, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		chordSpinner.setAdapter(adapter);
		chordSpinner.setOnItemSelectedListener(new SpinnerActivity());

		Spinner scaleSpinner = (Spinner) findViewById(R.id.scales_spinner);
		ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,
				R.array.scales_array, android.R.layout.simple_spinner_item);
		adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		scaleSpinner.setAdapter(adapter2);
		scaleSpinner.setOnItemSelectedListener(new SpinnerActivity2());


		image = (ImageView) findViewById(R.id.image1);
		image.setImageResource(R.drawable.majorscale);
		image2 = (ImageView) findViewById(R.id.image2);
		image2.setImageResource(R.drawable.major);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.page_chords, menu);
		return true;
	}

	public class SpinnerActivity extends Activity implements OnItemSelectedListener {


		public void onItemSelected(AdapterView<?> parent, View view, 
				int pos, long id) {
			// An item was selected. You can retrieve the selected item using
			// parent.getItemAtPosition(pos)
			chord = parent.getItemAtPosition(pos).toString();
			if (chord.equals("Major")){
				image2.setImageResource(R.drawable.major);
			}
			else if (chord.equals("Minor")){
				image2.setImageResource(R.drawable.minor);
			}
			else if (chord.equals("Augmented")){
				image2.setImageResource(R.drawable.augmented);
			}
			else if (chord.equals("Diminished")){
				image2.setImageResource(R.drawable.diminished);
			}

		}

		public void onNothingSelected(AdapterView<?> parent) {

		}
	}

	public class SpinnerActivity2 extends Activity implements OnItemSelectedListener {


		public void onItemSelected(AdapterView<?> parent, View view, 
				int pos, long id) {
			// An item was selected. You can retrieve the selected item using
			// parent.getItemAtPosition(pos)
			scale = parent.getItemAtPosition(pos).toString();
			if (scale.equals("Major")){
				image.setImageResource(R.drawable.majorscale);
			}
			else if (scale.equals("Natural Minor")){
				image.setImageResource(R.drawable.naturalminor);
			}
			else if (scale.equals("Harmonic Minor")){
				image.setImageResource(R.drawable.harmonicminor);
			}
			else if (scale.equals("Melodic Minor")){
				image.setImageResource(R.drawable.melodicminor);
			}

		}

		public void onNothingSelected(AdapterView<?> parent) {

		}
	}

	public void playScale(View v) {
		if (explosion!=0){
			if (scale.equals("Major")){
				LowC();
				time2();
				pause();
				D();
				time2();
				pause();
				E();
				time2();
				pause();
				F();
				time2();
				pause();
				G();
				time2();
				pause();
				A();
				time2();
				pause();
				B();
				time2();
				pause();
				HighC();
				time2();
				pause();
			}
			else if (scale.equals("Natural Minor")){
				LowC();
				time2();
				pause();
				D();
				time2();
				pause();
				DSharp();
				time2();
				pause();
				F();
				time2();
				pause();
				G();
				time2();
				pause();
				GSharp();
				time2();
				pause();
				ASharp();
				time2();
				pause();
				HighC();
				time2();
				pause();
			}
			else if (scale.equals("Harmonic Minor")){
				LowC();
				time2();
				pause();
				D();
				time2();
				pause();
				DSharp();
				time2();
				pause();
				F();
				time2();
				pause();
				G();
				time2();
				pause();
				GSharp();
				time2();
				pause();
				B();
				time2();
				pause();
				HighC();
				time2();
				pause();
			}
			else if (scale.equals("Melodic Minor")){
				LowC();
				time2();
				pause();
				D();
				time2();
				pause();
				DSharp();
				time2();
				pause();
				F();
				time2();
				pause();
				G();
				time2();
				pause();
				A();
				time2();
				pause();
				B();
				time2();
				pause();
				HighC();
				time2();
				pause();
				ASharp();
				time2();
				pause();
				GSharp();
				time2();
				pause();
			}
		}
	}
	public void playChord(View v) {
		if (explosion!=0){ 
			if (chord.equals("Major")){
				LowC();
				time();
				E();
				time();
				G();
				time();
			}
			else if (chord.equals("Minor")){
				LowC();
				time();
				DSharp();
				time();
				G();
				time();
			}
			else if (chord.equals("Augmented")){
				LowC();
				time();
				E();
				time();
				GSharp();
				time();
			}
			else if (chord.equals("Diminished")){
				LowC();
				time();
				DSharp();
				time();
				FSharp();
				time();
			}
		}
	}

	public void time(){
		int c = 0;
		while (c < 10000000){ //chord time
			c++;
		}
	}

	public void time2(){ //scale time
		int c = 0;
		while (c < 40000000){
			c++;
		}
	}

	public void pause(){
		sp.pause(explosion);
	}

	public void LowC(){
		sp.play(explosion, 1,1,0,0,1f);
	}
	public void CSharp(){
		sp.play(explosion, 1,1,0,0,1.0595f);
	}
	public void D(){
		sp.play(explosion, 1,1,0,0,1.1225f);
	}
	public void DSharp(){
		sp.play(explosion, 1,1,0,0,1.1892f);
	}
	public void E(){
		sp.play(explosion, 1,1,0,0,1.2599f);
	}
	public void F(){
		sp.play(explosion, 1,1,0,0,1.3348f);
	}
	public void FSharp(){
		sp.play(explosion, 1,1,0,0,1.4142f);
	}
	public void G(){
		sp.play(explosion, 1,1,0,0,1.4983f);
	}
	public void GSharp(){
		sp.play(explosion, 1,1,0,0,1.5874f);
	}
	public void A(){
		sp.play(explosion, 1,1,0,0,1.6818f);
	}
	public void ASharp(){
		sp.play(explosion, 1,1,0,0,1.7818f);
	}
	public void B(){
		sp.play(explosion, 1,1,0,0,1.8877f);
	}
	public void HighC(){
		sp.play(explosion, 1,1,0,0,2f);
	}
	
	public void changeSounds(View v) {
		Intent intent = new Intent(this, SoundsDialog.class);
		startActivityForResult(intent, 1);
	}
	
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch(requestCode) {
			case (1) : {
				if (resultCode == Activity.RESULT_OK) {
					name = data.getStringExtra("name");
					explosion = sp.load(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + name + ".mp3", 0);
					TextView text = (TextView) findViewById(R.id.textView_chords_file);
					text.setText(name+".mp3");
				}
				break;
			}
		}
	}

}
